package com.slkj.shilixiaoyuanapp.ui.teacher.tool.MyApproval;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.RepairDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.ChosePicAdapter;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.CHOSE_PIC_SPAN_COUNT;

@ActivityInfo(layout = R.layout.activity_sp_repair, title = "报修")

public class SpRepairActivity extends BaseActivity {
    @InjectParam
    int aId;
    @InjectParam
    int id;
    @InjectParam
    RepairDetailEntity data;
    @BindView(R.id.recyc_pic)
    RecyclerView recycerPic;

    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_now_day_time)
    TextView tvNowDayTime;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.recycer_spr)
    RecyclerView recycerSpr;
    @BindView(R.id.layout_botton)
    View viewBotton;
    @BindView(R.id.layout_reson)
    View viewReson;
    @BindView(R.id.edit_reson)
    EditText editReson;
    SqrAdapter sqrAdapter;
    ChosePicAdapter adapter;
    @Override
    protected void init() {
        SpRepairActivity__JumpCenter.bind(this);
        tvUserName.setText(UserRequest.getInstance().getUser().getUser().getUName());
        tvNowDayTime.setText(data.getIntime());

        if (data.getImages()!= null && !data.getImages().isEmpty()){
            recycerPic.setLayoutManager(new GridLayoutManager(this, CHOSE_PIC_SPAN_COUNT));
            adapter = new ChosePicAdapter(data.getImages(), this,false);
            recycerPic.setAdapter(adapter);
        }
        editContent.setText(data.getBody());
        editName.setText(data.getEquipmentName());
        editReson.setText(data.getRefute());
        if (data.getAuditor() != null && !data.getAuditor().isEmpty()){
            recycerSpr.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));
            recycerSpr.setLayoutManager(new LinearLayoutManager(this));

            sqrAdapter = new SqrAdapter(data.getAuditor());
            recycerSpr.setAdapter(sqrAdapter);
        }
        switch (data.getState()){
            case MyApprovalHistoryFragment.TYPE_NOT_LOOK:
                viewBotton.setVisibility(View.VISIBLE);
                break;
            case MyApprovalHistoryFragment.TYPE_REFUSE:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
            case MyApprovalHistoryFragment.TYPE_OK:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
        }
        viewBotton.setVisibility(data.getMyState() == MyApprovalHistoryFragment.TYPE_NOT_LOOK?View.VISIBLE:View.GONE);


    }
    @OnClick(R.id.tv_tg)void tg(){
        String reson = editReson.getText().toString();
        upData(reson,2);

    }

    private void upData(String reson,int state) {
        HttpHeper.get().toolService()
                .spDo(id,aId,state,reson)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true, this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        //showToast(data);
                        LoadSuccessAndFailDialog.showSuccess(SpRepairActivity.this,data);
                        finish();
                    }
                });
    }

    @OnClick(R.id.tv_wtg)void wtg(){
        String reson = editReson.getText().toString();
        if (TextUtils.isEmpty(reson)){
            showToast("请填写拒绝原因");
            return;
        }
        upData(reson,1);
    }
}
